package com.yyj.controller;

import com.github.pagehelper.PageInfo;
import com.yyj.entity.Admin;
import com.yyj.entity.Params;
import com.yyj.entity.Result;
import com.yyj.service.AdminService;
import org.springframework.web.bind.annotation.*;


@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {

    final
    AdminService adminService;

    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }

    //登录
    @PostMapping("/login")
    //请求实体
    public Result Login(@RequestBody Admin admin) {
        Admin loginUser = adminService.login(admin);
        return Result.success(loginUser);

    }

    //注册
    @PostMapping("/register")
    public Result Register(@RequestBody Admin admin) {
        adminService.insertAdmin(admin);
        return Result.success();

    }
    //增加管理员信息和编辑管理员信息
    @PostMapping
    public Result insertAdmin(@RequestBody Admin admin) {

        if (admin.getId() == null) {
            adminService.insertAdmin(admin);
        } else {
            //修改
            adminService.updateAdmin(admin);
        }
        return Result.success();
    }


    @GetMapping("/search")
    public Result getBySearch(Params params) {
        PageInfo<Admin> info = adminService.getBySearch(params);
        return Result.success(info);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        adminService.delete(id);
        return Result.success();
    }
}
